﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Logic.Company;
using Job.Entity;
using Job.Web.Framework;
using Job.Logic.Common;

namespace Job.Web.userControls.common
{
    public partial class CompanySearcherList : System.Web.UI.UserControl
    {
        SearcherLogic logic = new SearcherLogic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCount();
                BindData();
            }
        }



        private void BindData()
        {
            try
            {
                int pageSize = pagerButtom.PageSize;
                int pageIdx = pagerButtom.CurrentPageIndex;

                MySession session = new MySession();
                SessionCompany company = session.GetCompanyFromSession();

                DataTable dt = logic.GetPageSearcherByAccount(company.CompanyId,company.LoginCode,pageSize,pageIdx);
                if (dt == null)
                {
                    ShowError("未查询到搜索器列表！");
                }
                else
                {
                    this.SearcherList.DataSource = dt;
                    this.SearcherList.DataBind();
                }
            }
            catch (Exception ex)
            {
                this.SearcherList.Visible = false;
                ShowError("查询搜索器列表失败！");
            }

        }

        private void BindCount()
        {
            try
            {
                MySession session = new MySession();
                SessionCompany company = session.GetCompanyFromSession();

                int count = logic.GetCountByAccount(company.CompanyId, company.LoginCode);
                if (count > 0)
                {
                    pagerButtom.RecordCount = count;
                    pagerButtom.DataBind();
                    pagerButtom.Visible = true;
                }
                else
                {
                    pagerButtom.Visible = false;
                }

            }
            catch (Exception ex)
            {
                this.SearcherList.Visible = false;
                ShowError("查询搜索器失败！");
            }
        }

        private void ShowError(string message)
        {
            this.SearcherList.Visible = false;
            this.msgTab.Visible = true;
            this.msgTd.InnerHtml = "<font color='#990000'>" + message + "</font>";
            this.pagerButtom.Visible = false;
        }

        protected void SearcherList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex > -1)
                {
                    try
                    {
                        LinkButton lbtDel = (LinkButton)e.Row.FindControl("lbtDelete");
                        if (lbtDel != null) lbtDel.Attributes.Add("onClick", "return confirm('是否确认删除？')");

                        Label lbIsSenior = (Label) e.Row.FindControl("lbIsSenior");
                        if ( lbIsSenior.Text.Trim().Equals("1")) 
                            lbIsSenior.Text = "高级人才";
                        else
                            lbIsSenior.Text = "";

                        Label lbPersonClass = (Label) e.Row.FindControl("lbPersonClass");
                        lbPersonClass.Text = MemoryDictionary.GetCommonDictName(lbPersonClass.Text);

                        Label lbId = (Label)e.Row.FindControl("lbId");
                        HyperLink hlDirectSearcher = (HyperLink)e.Row.FindControl("hlSearcher");
                        HyperLink hlSearch = (HyperLink)e.Row.FindControl("hlSearcherBtn");

                        MySession session = new MySession();

                        if ( null != session.GetAdminUserFromSession())
                        {
                            string url = WebCommon.GetApplicationPath(Request)
                                + "AdminWeb/AdminMain.aspx?controlType="
                                + ((int)Constants.SubjectType.CompanySearcher).ToString()
                                + "&type=1&id=" + lbId.Text.Trim();
                            hlDirectSearcher.NavigateUrl = url;
                            hlSearch.NavigateUrl = url;
                        }
                        else
                        {
                        }

                    }
                    catch (Exception ex)
                    { }
                }
            }
        }

        protected void SearcherList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try 
            {
                int row = e.RowIndex;
                Label lbId = (Label)SearcherList.Rows[row].FindControl("lbId");
                string searcherId = lbId.Text;
                
                MySession session = new MySession();
                JobCompanySearcher searcher = new JobCompanySearcher();
                searcher.SearcherId = Convert.ToInt32(searcherId);
                searcher.CreatorCompany = session.GetCompanyFromSession().CompanyId;
                searcher.CreatorLogincode = session.GetCompanyFromSession().LoginCode;

                logic.DeleteSearcher(searcher);
                BindCount();
                BindData();
            }
            catch(Exception ex)
            {
                ShowError("删除搜索器失败：" + ex.Message);
            }
        }

        protected void SearcherList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int row = e.NewEditIndex;
            Label lbId = (Label)SearcherList.Rows[row].FindControl("lbId");
            string searcherId = lbId.Text;
            if (null == searcherId || searcherId.Equals("")) return;

            string url = WebCommon.GetApplicationPath(Request)
                + "AdminWeb/AdminMain.aspx?controlType="
                + ((int)Constants.SubjectType.CompanySearcher).ToString()
                + "&type=2&id=" + searcherId;
            Response.Redirect(url);
        }

        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}